package org.mch.func.file.filename;

import java.io.File;
import java.util.HashMap;
import java.util.Map;

public class MFileNameUtils {

	/**
	 * 插入指定名称字符
	 */
	public void insertFileName(File file, int insertIndex, String insertStr) {
		File[] files = file.listFiles();

		Map<File, File> fileRenameMap = new HashMap<File, File>();

		for (File tmpfile : files) {
			if (tmpfile.isFile() && !tmpfile.isHidden()) {
				String newFileName = new StringBuilder(tmpfile.getName()).insert(insertIndex - 1, insertStr).toString();
				File newfile = new File(tmpfile.getParent() + "\\" + newFileName);

				fileRenameMap.put(tmpfile, newfile);
			} else if (tmpfile.isDirectory() && !tmpfile.isHidden()) {
				insertFileName(tmpfile, insertIndex, insertStr);
			}
		}

		renameFileMap(fileRenameMap);
	}

	/**
	 * 增加名称前缀
	 */
	public void addNamePrefix(String filePath, String prefix, boolean isFileAndFolderName) {
		if ((prefix != null && !prefix.trim().equals("")) && (filePath != null && !filePath.trim().equals(""))) {
			File file = new File(filePath);
			File[] files = file.listFiles();

			Map<File, File> fileRenameMap = new HashMap<File, File>();

			for (File tmpfile : files) {
				if (tmpfile.isFile() && !tmpfile.isHidden()) {
					File newfile = new File(tmpfile.getParent() + "\\" + prefix + tmpfile.getName());

					fileRenameMap.put(tmpfile, newfile);
				} else if (tmpfile.isDirectory() && isFileAndFolderName && !tmpfile.isHidden()) {
					File newfile = new File(tmpfile.getParent() + "\\" + prefix + tmpfile.getName());

					fileRenameMap.put(tmpfile, newfile);
				}
			}

			renameFileMap(fileRenameMap);
		} else {
			//
		}
	}

	/**
	 * 增加名称后缀
	 */
	public void addNameSuffix(String filePath, String suffix, boolean isFileAndFolderName) {
		if ((suffix != null && !suffix.trim().equals("")) && (filePath != null && !filePath.trim().equals(""))) {
			File file = new File(filePath);
			File[] files = file.listFiles();

			Map<File, File> fileRenameMap = new HashMap<File, File>();

			for (File tmpfile : files) {
				if (tmpfile.isFile() && !tmpfile.isHidden()) {
					String oldname = tmpfile.getName();
					String newName = null;
					if (oldname.lastIndexOf(".") > -1) {
						String name = oldname.substring(0, oldname.lastIndexOf("."));
						String type = oldname.substring(oldname.lastIndexOf("."), oldname.length());
						newName = name + suffix + type;
					} else {
						newName = oldname + suffix;
					}
					File newfile = new File(tmpfile.getParent() + "\\" + newName);

					fileRenameMap.put(tmpfile, newfile);
				} else if (tmpfile.isDirectory() && isFileAndFolderName && !tmpfile.isHidden()) {
					String oldname = tmpfile.getName();
					String newName = null;
					newName = oldname + suffix;
					File newfile = new File(tmpfile.getParent() + "\\" + newName);

					fileRenameMap.put(tmpfile, newfile);
				}
			}

			renameFileMap(fileRenameMap);
		} else {

		}
	}

	/**
	 * 替换文件名某字符串；
	 * 
	 * @param old_str
	 * @param new_str
	 * @param folderPath
	 * @param contanfloder
	 */
	public void reqlaceFileName(String old_str, String new_str, File folder, boolean isUpdateFloder, boolean isSearchChild) {
		if ((old_str != null) && (folder != null)) {
			File[] files = folder.listFiles();

			for (File tmpfile : files) {
				if (tmpfile.isFile() && !tmpfile.isHidden()) {
					// 如果不是隐藏的文件则进行修改
					String oldname = tmpfile.getName();
					String newName = oldname.replace(old_str, new_str);

					File newfile = new File(tmpfile.getParent() + "\\" + newName);
					tmpfile.renameTo(newfile);
				}
				if (tmpfile.isDirectory() && !tmpfile.isHidden()) {
					if (isSearchChild) {// 如果搜索子文件夹，则先进行文件夹内的文件修改
						reqlaceFileName(old_str, new_str, tmpfile, isUpdateFloder, isSearchChild);
					}

					// 如果要更新文件夹，则搜索子文件夹后，修改当前文件夹
					if (isUpdateFloder) {
						String oldname = tmpfile.getName();
						String newName = oldname.replace(old_str, new_str);

						File newfile = new File(tmpfile.getParent() + "\\" + newName);
						tmpfile.renameTo(newfile);
					}
				}
			}

		} else {
			//
		}
	}

	protected void renameFileMap(Map<File, File> map) {
		if (map == null || map.isEmpty()) {
			return;
		}
		File[] keyfiles = map.keySet().toArray(new File[0]);
		for (int i = 0, len = keyfiles.length; i < len; i++) {
			keyfiles[i].renameTo(map.get(keyfiles[i]));
		}
	}

	/**
	 * 修改文件名称
	 */
	public void editFileName(File oldfile, File newfile) {
		oldfile.renameTo(newfile);
	}

	/**
	 * 修改单个文件类型
	 */
	public void editSigleFileType(File file, String oldTypeName, String newTypeName) {
		if (file == null || !file.exists() || !file.canRead()) {
			return;
		}

		if (file.isFile() && file.getName().endsWith(oldTypeName)) {
			String oldname = file.getPath();
			String name = oldname.substring(0, oldname.lastIndexOf("."));
			String type = oldname.substring(oldname.lastIndexOf(".") + 1, oldname.length());
			if (type.equalsIgnoreCase(oldTypeName)) {
				String newName = name + "." + newTypeName;
				file.renameTo(new File(newName));
			}
		}
	}

	/**
	 * 修改文件类型
	 */
	public void editFileType(File file, String oldTypeName, String newTypeName, boolean isContainChild) {
		if (file == null || !file.exists() || !file.canRead()) {
			return;
		}

		if (file.isFile() && file.getName().endsWith(oldTypeName)) {
			String oldname = file.getPath();
			String name = oldname.substring(0, oldname.lastIndexOf("."));
			String type = oldname.substring(oldname.lastIndexOf(".") + 1, oldname.length());
			if (type.equalsIgnoreCase(oldTypeName)) {
				String newName = name + "." + newTypeName;
				file.renameTo(new File(newName));
			}
		} else if (file.isDirectory() && isContainChild) {
			File[] files = file.listFiles();
			for (File tmpfile : files) {
				editFileType(tmpfile, oldTypeName, newTypeName, isContainChild);
			}
		}
	}
}
